package com.fake

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.viewModels
import com.business.Analyse
import com.business.AnalyseKey
import com.cy.game.CyHomeActivity
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.activitys.*
import com.helloandroid.base.BaseFragment
import com.helloandroid.dialogs.AwardDialog
import com.helloandroid.models.ZouViewModel
import com.helloandroid.tools.GUtils
import com.helloandroid.tools.MyTimeUtils
import com.helloandroid.tools.NetWorkUtil
import com.helloandroid.tools.ThrottleClickListener
import com.helloandroid.view.DashBoard
import com.helloandroid.vo.BoardInfo
import com.helloandroid.vo.ZouConstants
import dai.ui.UITweenModel
import dai.ui.moveBy
import dai.ui.repeat
import dai.ui.single.Delay
import dai.ui.single.MoveBy
import game.engine.base.CoreUtil

class FakeZouFragment : BaseFragment() {

    val model: ZouViewModel by viewModels()
    private val tweenModel: UITweenModel by viewModels()
    var isReady = false
    var mInfo: BoardInfo? = null

    // views
    lateinit var tvCount: TextView
    lateinit var dashBoard: DashBoard
    private lateinit var iv_coin_1: ImageView
    private lateinit var iv_coin_2: ImageView
    private lateinit var iv_coin_3: ImageView
    private lateinit var iv_dailyCoin: ImageView
    lateinit var tvTargetBs: TextView

    //
    lateinit var tvDistance: TextView
    lateinit var tvSpendTime: TextView
    lateinit var tvEnergy: TextView

    private val throttleListeren = object: ThrottleClickListener() {
        override fun noDoubleClick(v: View?) {
            when(v?.id){
                //  补贴
                R.id.ibZaoWanDaKa -> startToActivity(ZaoWanDaKaActivity::class.java)
                R.id.ibHeShuiDaKa -> startToActivity(HeShuiDaKaActivity::class.java)
                R.id.ibChiFanBuTie -> startToActivity(ChiFanActivity::class.java)
                R.id.ibMeiRiYunDong -> startToActivity(MeiRiYunDongActivity::class.java)
                R.id.ibBuShuDaKa -> startToActivity(BuShuDaKaActivity::class.java)

                // 步数打卡. 跳转目标
                R.id.btnBs -> if (isReady)startToActivity(BuShuDaKaActivity::class.java)
            }
        }
    }

    override fun getLayoutResId(): Int = R.layout.fragment_fake_zou

    override fun initView(view: View) {
        tweenModel.startLoop()
        tvCount = findViewById(R.id.tvCount)
        dashBoard = findViewById(R.id.dashBoard)
        iv_coin_1 = findViewById(R.id.iv_coin_1)
        iv_coin_2 = findViewById(R.id.iv_coin_2)
        iv_coin_3 = findViewById(R.id.iv_coin_3)
        iv_dailyCoin = findViewById(R.id.iv_dailyCoin)
        tvTargetBs = findViewById(R.id.tvTargetBs)

        tvDistance = findViewById(R.id.tvDistance)
        tvEnergy = findViewById(R.id.tvEnergy)
        tvSpendTime = findViewById(R.id.tvSpendTime)

        coinAnim(iv_coin_1)
        coinAnim(iv_coin_2)
        coinAnim(iv_coin_3)
        coinAnim(iv_dailyCoin)

        findBtn(R.id.ibZaoWanDaKa).setOnClickListener(throttleListeren)
        findBtn(R.id.ibHeShuiDaKa).setOnClickListener(throttleListeren)
        findBtn(R.id.ibChiFanBuTie).setOnClickListener(throttleListeren)
        findBtn(R.id.ibMeiRiYunDong).setOnClickListener(throttleListeren)
        findBtn(R.id.ibBuShuDaKa).setOnClickListener(throttleListeren)
        findBtn(R.id.btnBs).setOnClickListener(throttleListeren)

        // for test
//        findBtn(R.id.btnTest).setOnClickListener {
//            startToActivity(CyHomeActivity::class.java)
//        }
    }

    override fun initData() {
        model.getInfo {success,info->
            isReady = success
            if (isReady) {
                mInfo = info
                // 更新UI
                initUI()
            } else {
                AppUtil.toast("加载信息失败!!")
            }
        }
        model.startPerSecondTick(this::perSecondTick)
    }

    private fun perSecondTick(){
        if (!isReady){
            return
        }

        // 更新UI
        updateBuShu(AppUtil.user.bs)

        // 更新目标
        tvTargetBs.text = "目标${ AppUtil.user.bs_target}步"
    }
    private fun initUI(){
        val info = mInfo!!
        updateFreeCoin1(info.cointime1)
        updateFreeCoin2(info.cointime2)
        updateFreeCoin3(info.cointime3)
        updateFreeCoin4(info.dailyCoinState)
        updateBuShu(AppUtil.user.bs)
    }

    private fun coinAnim(coin: View) {
        val move = MoveBy(1f,0f,20f)
                .moveBy(1f,0f,-20f)

        val time = CoreUtil.randomFloat(3f,6f) / 10f
        Delay(time).repeat(-1,move).runBy(coin,tweenModel)
    }

    ////=========== update UI
    private fun updateFreeCoin1(lastTime:Long){
        if (AppUtil.sysTime - lastTime >= GUtils.OneHourMs) {
            // 显示出来
            iv_coin_1.visibility = View.VISIBLE
            iv_coin_1.setImageResource(R.drawable.dd_coin_small)
            iv_coin_1.setOnClickListener {
                btnGetFreeCoin(ZouConstants.FreeCoin_1)
            }
        } else {
            iv_coin_1.visibility = View.INVISIBLE
        }
    }

    private fun updateFreeCoin2(lastTime:Long){
        if (AppUtil.sysTime - lastTime >= GUtils.OneHourMs) {
            // 显示
            iv_coin_2.visibility = View.VISIBLE
            iv_coin_2.setImageResource(R.drawable.dd_coin_small)
            iv_coin_2.setOnClickListener {
                btnGetFreeCoin(ZouConstants.FreeCoin_2)
            }
        } else {
            iv_coin_2.visibility = View.INVISIBLE
        }
    }

    private fun updateFreeCoin3(lastTime:Long){
        if (AppUtil.sysTime - lastTime >= GUtils.TwoHourMs) {
            // 显示
            iv_coin_3.visibility = View.VISIBLE
            iv_coin_3.setImageResource(R.drawable.dd_coin_small)
            iv_coin_3.setOnClickListener {
                btnGetFreeCoin(ZouConstants.FreeCoin_3)
            }
        } else {
            iv_coin_3.visibility = View.INVISIBLE
        }
    }

    private fun updateFreeCoin4(state:Int){
        // 0 表示还没领取
        // 1 表示已经领取
        // 每日红包奖励是否已经领了
        if (state < 1) {
            // 还没有领取
            iv_dailyCoin.visibility = View.VISIBLE
            iv_dailyCoin.setOnClickListener {
                btnGetFreeCoin(ZouConstants.FreeCoin_4)
            }
        } else {
            iv_dailyCoin.visibility = View.INVISIBLE
        }
    }

    fun updateBuShu(bushu: Int) {
        tvCount.text = bushu.toString()
        dashBoard.updateProgress(bushu)
        updateTodayData()

        if (!GUtils.dashBoardAnimated){
            GUtils.dashBoardAnimated = true
            dashBoard.anim()
        }
    }
    private fun updateTodayData() {
        val user = AppUtil.user
        // 每步 0.52m
        // 每秒 1.66步
        // 每步 消耗 0.03千卡
        tvDistance.text = String.format("%.2f", user.bs * GUtils.MeterPerBu / 1000f)

        // 超过1个小时,就使用  18sp
        val timeSeconds = (user.bs / 1.66f).toLong()
        if (timeSeconds < 3600) {
            tvSpendTime.text = MyTimeUtils.format_mmss(timeSeconds * 1000)
        } else {
            tvSpendTime.text = MyTimeUtils.format_HHmmss(timeSeconds * 1000)
        }

        // 卡路里
        tvEnergy.text = String.format("%.2f", user.bs * 0.03f)
    }

    private fun btnGetFreeCoin(type: Int) {

        if (!NetWorkUtil.networkConnected()){
            AppUtil.toast("请打开网络!")
            return
        }

        Analyse.report(AnalyseKey.DashBoardCoin)

        // 1和2为小额
        // 3为大额
        // 4 也是金币
        when (type) {
            ZouConstants.FreeCoin_1 ->{
                // 小额
                var awardCoin = GUtils.randomInt(8,40)
                AppUtil.appViewModel().zouAwardFreeCoin(type,awardCoin)
                mInfo?.apply {
                    cointime1 = AppUtil.sysTime
                    updateFreeCoin1(cointime1)
                }
                showAwardDialog(awardCoin, AnalyseKey.DashBoardCoin_Video)
            }
            ZouConstants.FreeCoin_2 -> {
                // 小额
                var awardCoin = GUtils.randomInt(8,40)
                AppUtil.appViewModel().zouAwardFreeCoin(type,awardCoin)
                mInfo?.apply {
                    cointime2 = AppUtil.sysTime
                    updateFreeCoin2(cointime2)
                }
                showAwardDialog(awardCoin, AnalyseKey.DashBoardCoin_Video)
            }

            ZouConstants.FreeCoin_3 ->{
                //  大额
                var awardCoin = GUtils.randomInt(50,80)
                AppUtil.appViewModel().zouAwardFreeCoin(type,awardCoin)
                mInfo?.apply {
                    cointime3 = AppUtil.sysTime
                    updateFreeCoin3(cointime3)
                }
                showAwardDialog(awardCoin, AnalyseKey.DashBoardCoin_Video)
            }
            ZouConstants.FreeCoin_4->{
                var awardCoin = 88
                AppUtil.appViewModel().zouAwardFreeCoin(type,awardCoin)
                mInfo?.apply {
                    dailyCoinState = 1
                    updateFreeCoin4(dailyCoinState)
                }
                showAwardDialog(awardCoin, AnalyseKey.DashBoardCoin_Video)
            }
        }
    }
    private fun showAwardDialog(coinCount:Int,reasonKey:String){
        val dialog = AwardDialog(this, coinCount)
        dialog.reasonKey = reasonKey
        dialog.show()
    }
    private fun <T : Activity> startToActivity(clz: Class<T>) {
        startActivity(Intent(activity, clz))
    }
}